package com.comtime.usercenter;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.comtime.greendaodemo.AdminInfo;
import com.comtime.greendaodemo.MemberInfo;
import com.comtime.greendaoutils.DataBaseUtil;
import com.comtime.service.MyService;
import com.comtime.smartbox.BaseActivity;
import com.comtime.smartbox.HBApplication;
import com.comtime.smartkilimanjaro.R;
import com.comtime.utils.LogUtils;
import com.comtime.utils.MyConfig;
import com.comtime.utils.MySharedPreferences;
import com.comtime.utils.Util;
import com.comyou.comyouhttp.ComyouHttpCallBack;
import com.comyou.comyouhttp.ComyouHttpClient;
import com.comyou.comyouhttp.ComyouHttpProgram;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;

import static com.comtime.smartkilimanjaro.R.id.tv_phonenum;

public class AddChildActivity extends BaseActivity {

    private String TAG = "AddChildActivity";

    EditText edt_phonenum;
    String selectedMac;
    MySharedPreferences mySharedPreferences;
    HBApplication application;
    DataBaseUtil dataBaseUtil;


    boolean isProgressing;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_child);
        mySharedPreferences = MySharedPreferences.getInstance(this);
        application = (HBApplication) getApplication();
        dataBaseUtil = DataBaseUtil.getInstance(this);
        init();
        regist();

    }

    void regist() {
        IntentFilter filter = new IntentFilter(
                MyService.ACTION_DISCONNECTED);
        filter.addAction(MyService.ACTION_ADD_USER_RESULT);
        registerReceiver(receiver, filter);
    }

    void init() {
        edt_phonenum = (EditText) findViewById(R.id.edt_phonenum);


    }

    public void BackAction(View v) {
        finish();
        overridePendingTransition(R.anim.enter_left, R.anim.exit_right);
    }

    String action = "";
    BroadcastReceiver receiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context arg0, Intent intent) {
            action = intent.getAction();
            if (action.equals(MyService.ACTION_DISCONNECTED)) {
                handler.sendEmptyMessage(1);

            } else if (action.equals(MyService.ACTION_ADD_USER_RESULT)) {

                int add_result_num = intent
                        .getIntExtra(MyService.VALUE_INFO, 0);
                Message message = new Message();
                message.what = 2;
                message.obj = add_result_num;
                handler.sendMessage(message);

            }

        }
    };

    /**
     * 添加子账号
     *
     * @param v
     */
    String phoneNum = "";

    public void CommitAction(View v) {

        if (!Util.hasNetwork(AddChildActivity.this)) {
            showToast(getText(R.string.no_network).toString());
            return;
        }
        if (TextUtils.isEmpty(edt_phonenum.getText())) {
            showToast(getText(R.string.input_phone).toString());
            return;
        }
        if (!Util.isMobileNO(edt_phonenum.getText().toString())) {
            showToast(getText(R.string.phone_format_wrong).toString());
            return;
        }
        if (isProgressing) {
            LogUtils.e(TAG, " isProgressing  return");


            return;
        }

        isProgressing = true;
        phoneNum = edt_phonenum.getText().toString();
		application.swDevicesList.get(0).sendAddUserPhone(phoneNum);

    }

    Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case 1:
                    isProgressing = false;
                    showToast(getText(R.string.device_dis_connect).toString());
                    AddChildActivity.this.finish();
                    overridePendingTransition(R.anim.enter_left, R.anim.exit_right);

                    break;
                case 2:
                    // 添加用户结果
                    int add_result_num = (Integer) msg.obj;
                    if (add_result_num == 0) {
                        isProgressing = false;
                        showToast(getString(R.string.add_child_fail).toString());
                    } else {

                        LogUtils.e(TAG, " ADD_CHILD 1111");

                        showToast(getString(R.string.add_child_success).toString());
                        Intent intent = new Intent(AccountManagerActivity.ADD_CHILD);
                        intent.putExtra(MyService.VALUE_INFO, phoneNum);
                        sendBroadcast(intent);
                        handler.postDelayed(new Runnable() {

                            @Override
                            public void run() {
                                isProgressing = false;
                                AddChildActivity.this.finish();
                                overridePendingTransition(R.anim.enter_left,
                                        R.anim.exit_right);
                            }
                        }, 1000);
                    }

                    break;

                default:
                    break;
            }
        }

        ;
    };

    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(receiver);

    }

    ;

    /**
     * 自定义提示
     *
     * @param str
     */
    Toast toast = null;

    void showToast(String str) {
        if (toast != null) {
            toast.cancel();
        }
        LayoutInflater inflater = getLayoutInflater();
        View view = inflater.inflate(R.layout.toastshow, null);
        TextView textView = (TextView) view.findViewById(R.id.toastshow);
        textView.setText(str);
        toast = new Toast(this);
        double d = getResources().getDisplayMetrics().density;
        toast.setGravity(Gravity.FILL_HORIZONTAL | Gravity.CENTER, 0, 0);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setView(view);
        toast.show();
    }

}
